<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib  prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib  prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="redis" uri="http://www.shopjsp.com/redisTag/functions" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${redis:getSysConfig('base_path_back')}"/>
<c:set var="srcPath" value="${redis:getSysConfig('sysFileVisitRoot_back')}"/>
<c:set var="uploadPath" value="${redis:getSysConfig('uploadFileVisitRoot')}"/>
<c:set var="defaultImg" value="${redis:getSysConfig('website_default_img')}"/>
<script type="text/javascript">
    /**
     * 查询分类已关联品牌
     */
	<shiro:hasPermission name="back_gotoProductTypePage_queryBrand">
    function queryBrandInfo(){
        var selectedRow= $("#tt").bootstrapTable('getSelections');
        if(selectedRow.length==1){
            var levelCode=selectedRow[0].levelCode;
            if(levelCode==3){
                $("#productTypeId").val(selectedRow[0].productTypeId);/**获取到productTypeId值**/
                $("#btn_queryBrand").attr("data-target", "#queryBrandModal");/**当前分类级别是三级分类才可打开对应的模态框**/
                $("#queryBrandModalLabel").empty().text("分类已关联品牌");
                /** 重置当前未选中的列表中的查询条件**/
                $("#qform_queryBrand")[0].reset();
                showBrandList();
            }else{
                $("#btn_queryBrand").attr("data-target",""); /**没有选中记录不可打开模态框**/
                showMsg("当前分类级别不是三级分类，不可查询品牌！");
            }
        }else{
            $("#btn_queryBrand").attr("data-target",""); /**没有选中记录不可打开模态框**/
            showMsg("请选择一行记录！");
        }
    }
	</shiro:hasPermission>

    /**
     * 分类已关联品牌列表
     */
    function showBrandList(){
       $("#tt_brandList").bootstrapTable('destroy'); 
        var $tt_brandList = $("#tt_brandList").bootstrapTable({
            url: '${basePath}/back/brandType/listSelectedBrandByProductTypeId.sjson',      	/**请求后台的URL（*）**/
            method: 'post',                       	/**请求方式（*）**/
            contentType: "application/x-www-form-urlencoded",
            dataType: 'json',
            toolbar: '#toolbar2',               	    /**工具按钮用哪个容器**/
            striped: false,                      	/**是否显示行间隔色**/
            cache: false,                       	/**是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）**/
            pagination: true,                   	/**是否显示分页（*）**/
            sortable: false,                    	/**是否启用排序**/
            sortOrder: 'asc',                   	/**排序方式**/
            queryParamsType: '',
            sidePagination: 'server',           	/**分页方式：client客户端分页，server服务端分页（*）**/
            pageNumber: 1,
            pageSize:10,							/**初始化加载第一页，默认第一页**/
            pageList: [5,10, 25, 50],        	    /**可供选择的每页的行数（*）**/
            search: false,                       	/**是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大**/
            strictSearch: true,
            showColumns: true,                  	/**是否显示所有的列**/
            showRefresh: true,                  	/**是否显示刷新按钮**/
            minimumCountColumns: 2,             	/**最少允许的列数**/
            clickToSelect: true,                	/**是否启用点击选中行**/
            uniqueId: 'brandId',                  	/**每一行的唯一标识，一般为主键列**/
            singleSelect  : true,                   /** 单选checkbox**/
            showToggle:false,                    	/**是否显示详细视图和列表视图的切换按钮**/
            cardView: false,                    	/**是否显示详细视图**/
            detailView: false,                  	/**是否显示父子表**/
            columns: [
                {checkbox: true},
                {field: 'Number', title: '编号', align: 'center', valign: 'middle', width: '50px',
                    formatter: function (value, row, index) {
                        var page = $tt_brandList.bootstrapTable("getPage");
                        return page.pageSize * (page.pageNumber - 1) + index + 1;
                    }
                },
                {field: 'brandBigImageUrl', title: '品牌图片', align: 'center', valign: 'middle',
                    formatter: function (value, row, index) {
                        var a_img_html = '<div><img id="uploadImg_' + (index+10) + '" class="uploadImg" onMouseOver="showListBigImage(' + (index+10) + ',4,4,0,-40,\'tt_brandList\')" onMouseOut="showListRebackImage(' + (index+10) + ',1,1,\'tt_brandList\')"  src="${uploadPath}/' + row.brandBigImageUrl + '" onerror="this.src=\'${srcPath}/common/images/mrbj.png\'"  width="30px" height="30px"/></div>';
                        return a_img_html;
                    }
                },
                {field: 'brandName', title: '品牌名称', align: 'center', valign: 'middle'},
                {field: 'isShow', title: '是否显示', align: 'center', valign: 'middle',
                    formatter: function (value, row, index) {
                        if (value == "0") {
                            return "<font class='color_002'>未显示</font>";
                        }
                        if (value == "1") {
                            return "<font class='color_001'>已显示</font>";
                        }
                    }
                },
                {field: 'isRecommend', title: '品牌墙推荐', align: 'center', valign: 'middle',
                    formatter: function (value, row, index) {
                        if (value == "0") {
                            return "<font class='color_002'>未推荐</font>";
                        }
                        if (value == "1") {
                            return "<font class='color_001'>已推荐</font>";
                        }
                    }
                },
                {field: 'isHomePage', title: '店铺品牌广告', align: 'center', valign: 'middle',
                    formatter: function (value, row, index) {
                        if (value == "0") {
                            return "<font class='color_002'>未推荐</font>";
                        }
                        if (value == "1") {
                            return "<font class='color_001'>已推荐</font>";
                        }
                    }
                }
            ],
            queryParams: function (params) {
                /**特别说明，返回的参数的值为空，则当前参数不会发送到服务器端,这种指定请求参数的方式和datatables控价类似**/
                params.brandName = $("#q_brandName").val();
                params.isShow = $("#q_isShow").val();
                params.productTypeId = $("#productTypeId").val();
                params.currentPage = params.pageNumber;
                return params;
            }
        });

        /**表单查询**/
        $("#btn_query_queryBrand").click(function () {
            $("#tt_brandList").bootstrapTable('refresh');
        });
    }

    /**
     * 添加分类未关联品牌
     */
    function addUnselectBrandInfo(){
        var selectedRow= $("#tt").bootstrapTable('getSelections');
        if(selectedRow.length==1){
            var levelCode=selectedRow[0].levelCode;
            if(levelCode==3){
                var selectedRow= $("#tt").bootstrapTable('getSelections');
                $("#form_assign").find("#productTypeId").val(selectedRow[0].productTypeId);/**获取到productTypeId值并放入隐藏域**/
                $("#btn_add_queryBrand").attr("data-target", "#queryUnselectBrandModal");/**当前分类级别是三级分类才可打开对应的模态框**/
                $("#queryUnselectBrandModalLabel").empty().text("分类未关联品牌");
                /** 重置当前未关联的列表中的查询条件**/
                $("#qform_queryUnselectBrand")[0].reset();
                /** 刷新当前未关联的列表**/
                $("#tt_brandTypeList").bootstrapTable('refresh');
                showUnselectBrandList();
            }else{
                $("#btn_add_queryBrand").attr("data-target",""); /**没有选中记录不可打开模态框**/
                showMsg("当前分类级别不是三级分类，不可查询品牌！");
            }
        }else{
            $("#btn_add_queryBrand").attr("data-target",""); /**没有选中记录不可打开模态框**/
            showMsg("请选择一行记录！");
        }
    }

    /**
     * 添加未关联分类品牌列表
     */
    function showUnselectBrandList(){
        var $tt_brandTypeList = $("#tt_brandTypeList").bootstrapTable({
            url: '${basePath}/back/brandType/listUnselectedBrandByProductTypeId.sjson',         	/**请求后台的URL（*）**/
            method: 'post',                       	/**请求方式（*）**/
            contentType: "application/x-www-form-urlencoded",
            dataType: 'json',
            //toolbar: '#toolbar',               	    /**工具按钮用哪个容器**/
            striped: true,                      	/**是否显示行间隔色**/
            cache: false,                       	/**是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）**/
            pagination: true,                   	/**是否显示分页（*）**/
            sortable: false,                    	/**是否启用排序**/
            sortOrder: 'asc',                   	/**排序方式**/
            queryParamsType: '',
            sidePagination: 'server',           	/**分页方式：client客户端分页，server服务端分页（*）**/
            pageNumber: 1,
            pageSize:10,							/**初始化加载第一页，默认第一页**/
            pageList: [5,10, 25, 50],        	    /**可供选择的每页的行数（*）**/
            search: false,                       	/**是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大**/
            strictSearch: true,
            showColumns: false,                  	/**是否显示所有的列**/
            showRefresh: false,                  	/**是否显示刷新按钮**/
            minimumCountColumns: 2,             	/**最少允许的列数**/
            clickToSelect: true,                	/**是否启用点击选中行**/
            uniqueId: 'brandId',                  	/**每一行的唯一标识，一般为主键列**/
            singleSelect  : false,                 /**单选checkbox**/
            showToggle:false,                    	/**是否显示详细视图和列表视图的切换按钮**/
            cardView: false,                    	/**是否显示详细视图**/
            detailView: false,                  	/**是否显示父子表**/
            columns: [
                {checkbox: true},
                {field: 'Number', title: '编号', align: 'center', valign: 'middle', width: '50px',
                    formatter: function (value, row, index) {
                        var page = $tt_brandTypeList.bootstrapTable("getPage");
                        return page.pageSize * (page.pageNumber - 1) + index + 1;
                    }
                },
                {field: 'brandBigImageUrl', title: '品牌图片', align: 'center', valign: 'middle',
                    formatter: function (value, row, index) {
                        var a_img_html = '<div><img id="uploadImg_' + (index+20) + '" class="uploadImg" onMouseOver="showListBigImage(' + (index+20) + ',4,4,0,-40,\'tt_brandTypeList\')" onMouseOut="showListRebackImage(' + (index+20) + ',1,1,\'tt_brandTypeList\')"  src="${uploadPath}/' + row.brandBigImageUrl + '" onerror="this.src=\'${srcPath}/common/images/mrbj.png\'"  width="30px" height="30px"/></div>';
                        return a_img_html;
                    }
                },
                {field: 'brandName', title: '品牌名称', align: 'center', valign: 'middle'},
                {field: 'isShow', title: '是否显示', align: 'center', valign: 'middle',
                    formatter: function (value, row, index) {
                        if (value == "0") {
                            return "<font class='color_002'>未显示</font>";
                        }
                        if (value == "1") {
                            return "<font class='color_001'>已显示</font>";
                        }
                    }
                },
                {field: 'isRecommend', title: '品牌墙推荐', align: 'center', valign: 'middle',
                    formatter: function (value, row, index) {
                        if (value == "0") {
                            return "<font class='color_002'>未推荐</font>";
                        }
                        if (value == "1") {
                            return "<font class='color_001'>已推荐</font>";
                        }
                    }
                },
                {field: 'isHomePage', title: '店铺品牌广告', align: 'center', valign: 'middle',
                    formatter: function (value, row, index) {
                        if (value == "0") {
                            return "<font class='color_002'>未推荐</font>";
                        }
                        if (value == "1") {
                            return "<font class='color_001'>已推荐</font>";
                        }
                    }
                }
            ],
            queryParams: function (params) {
                /**特别说明，返回的参数的值为空，则当前参数不会发送到服务器端,这种指定请求参数的方式和datatables控价类似**/
                params.brandName = $("#q_sub_brandName").val();
                params.isShow = $("#q_sub_isShow").val();
                params.isRecommend = $("#q_sub_isRecommend").val();
                params.isHomePage = $("#q_sub_isHomePage").val();
                params.productTypeId = $("#productTypeId").val();
                params.currentPage = params.pageNumber;
                return params;
            }
        });

        /**表单查询**/
        $("#btn_query_queryUnselectBrand").click(function () {
            $("#tt_brandTypeList").bootstrapTable('refresh');
        });
    }

    /**保存添加的未关联分类品牌**/
    function saveBrandType(){
        /**获取选中行的id**/
        var selectedRow= $("#tt_brandTypeList").bootstrapTable('getSelections');
        var brandIds="";
        var isShows="";
        if(selectedRow.length>0){
            for(var i=0;i<selectedRow.length;i++){
                var  brandId=selectedRow[i].brandId;
                var  isShow=selectedRow[i].isShow;
                if(i==selectedRow.length-1){
                    brandIds+=brandId;
                    isShows+=isShow;
                }else{
                    brandIds+=brandId+",";
                    isShows+=isShow+",";
                }
            }
            /** 获取值放入隐藏域 **/
            $("#brandIds").val(brandIds);
            $("#isShows").val(isShows);
            var options = {
                url: $("#form_assign").attr("action"),
                type : "post",
                dataType:"json",
                success : function(data) {
                    if(data.success==true){
                        /** 先重置已关联品牌窗口查询框的值**/
                        $("#qform_queryBrand")[0].reset();
                        /** 刷新已关联品牌窗口的列表**/
                        $("#tt_brandList").bootstrapTable('refresh');
                        /** 隐藏未关联品牌弹出模态框窗口**/
                        $('#queryUnselectBrandModal').modal('hide');
                        showMsg("保存成功！");
                    }else{
                        showMsg("保存失败！");
                    }
                }
            };
            $("#form_assign").ajaxSubmit(options);
        }else{
            showMsg("请选择一行记录！");
        }
    }

    /**
     * 删除分类下的品牌
     */
    function deleteBrandInfo() {
        /**获取选中行的id**/
        var selectedRow = $("#tt_brandList").bootstrapTable('getSelections');
        var selectedRow_tt = $("#tt").bootstrapTable('getSelections');
        if (selectedRow.length == 1) {
            var productTypeId=selectedRow_tt[0].productTypeId;
            var brandId = selectedRow[0].brandId;
            showConfirm("你确定要删除吗?", function () {
                $.ajax({
                    type: "POST",
                    dataType: "JSON",
                    url: "${basePath}/back/brandType/deleteBrandType.sjson",
                    data: {productTypeId: productTypeId,brandId: brandId},
                    success: function (data) {
                        if (data.success == true) {
                            $("#tt_brandList").bootstrapTable('refresh');/**刷新已经选中的列表**/
                            $("#tt_brandTypeList").bootstrapTable('refresh');/**刷新未选中的列表**/
                        }
                    }
                });
            });
        } else {
            showMsg("请选择一行记录！");
        }
    }

    /**
     * 更改显示状态
     */
    function editRegisterInfo() {
        /**获取选中行的id**/
        var selectedRow = $("#tt_brandList").bootstrapTable('getSelections');
        var selectedRow_tt = $("#tt").bootstrapTable('getSelections');
        if (selectedRow.length == 1) {
            var productTypeId=selectedRow_tt[0].productTypeId;
            var brandId = selectedRow[0].brandId;
            showConfirm("你确定要更改显示状态吗?", function () {
                $.ajax({
                    type: "POST",
                    dataType: "JSON",
                    url: "${basePath}/back/brandType/updateIsShowState.sjson",
                    data: {productTypeId: productTypeId,brandId: brandId},
                    success: function (data) {
                        if (data.success == true) {
                            $("#tt_brandList").bootstrapTable('refresh');/**刷新已经选中的列表**/
                        }
                    }
                });
            });
        } else {
            showMsg("请选择一行记录！");
        }
    }

    /**
     * 复制上级分类品牌
     */
    function copyLastBrandInfo() {
        var selectedRow = $("#tt").bootstrapTable('getSelections');
        showConfirm("你确定要复制吗?", function () {
            $.ajax({
                type: "POST",
                dataType: "JSON",
                url: "${basePath}/back/brandType/copyParentProductType.sjson",
                data: {productTypeId: selectedRow[0].productTypeId},
                success: function (data) {
                    if (data.success == true) {
                        $("#tt_brandList").bootstrapTable('refresh');/**刷新已经选中的列表**/
                    }
                }
            });
        });
    }

</script>
<!--模态框（Modal） -->
<div class="modal fade" id="queryBrandModal" tabindex="-1" role="dialog" aria-labelledby="queryBrandModalLabel" aria-hidden="true" data-backdrop="static">
	<div class="modal-dialog" style="width:90%">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h5 class="modal-title" id="queryBrandModalLabel"></h5>
			</div>

			<div class="modal-body" style="padding-top: 5px;">
				<!-- 查询条件 -->
				<div class="panel panel-default" style="margin-top: 15px;">
					<div class="panel-body">
						<form id="qform_queryBrand" class="form-horizontal">
							<div class="form-group" style="margin-top:15px">
								<div class="col-sm-3" style="width: 220px;">
									<div class="input-group input-group-sm">
										<span class="input-group-addon">品牌名称</span>
										<input type="text" class="form-control" id="q_brandName" name="brandName" placeholder="请输入品牌名称"/>
									</div>
								</div>
								<div class="col-sm-2" style="width: 200px;">
									<div class="input-group input-group-sm">
										<span class="input-group-addon">是否显示</span>
										<select id="q_isShow" class="form-control">
											<option value="">--请选择--</option>
											<option value="0">未显示</option>
											<option value="1">已显示</option>
										</select>
									</div>
								</div>
								<div class="col-sm-2" style="width:160px;">
									<button type="button" id="btn_query_queryBrand" class="btn btn-primary">查询</button>&nbsp;&nbsp;
									<button type="reset" class="btn btn-success" id="resetBtn" >重置</button>
								</div>
							</div>
						</form>
					</div>
				</div>
				<!-- 工具栏 -->
				<div id="toolbar2" class="btn-group">
					<button id="btn_add_queryBrand" type="button" class="btn btn-default" data-toggle="modal" onclick="addUnselectBrandInfo()">
						<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>添加分类未关联品牌
					</button>
					<button id="btn_delete_queryBrand" type="button" class="btn btn-default" onclick="deleteBrandInfo()">
						<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
					</button>
					<button id="btn_edit_queryBrand" type="button" class="btn btn-default" data-toggle="modal" onclick="editRegisterInfo()">
						<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>更改显示状态
					</button>
					<button id="btn_copy_queryBrand" type="button" class="btn btn-default" data-toggle="modal" onclick="copyLastBrandInfo()">
						<span class="glyphicon glyphicon-save" aria-hidden="true"></span>复制上级分类品牌
					</button>
				</div>
				<form id="form_brandList"  method="post" action="">
					<!-- 渲染的table -->
					<table id="tt_brandList"></table>
				</form>
			</div>

			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>

<div class="modal fade" id="queryUnselectBrandModal" tabindex="-1" role="dialog" aria-labelledby="queryUnselectBrandModalLabel" aria-hidden="true" data-backdrop="static">
	<div class="modal-dialog" style="width:70%">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h5 class="modal-title" id="queryUnselectBrandModalLabel"></h5>
			</div>

			<div class="modal-body" style="padding-top: 5px;">
				<!-- 查询条件 -->
				<div class="panel panel-default" style="margin-top: 15px;">
					<div class="panel-body">
						<form id="qform_queryUnselectBrand" class="form-horizontal">
							<div class="form-group" style="margin-top:15px">
								<div class="col-sm-3" style="width: 220px;">
									<div class="input-group input-group-sm">
										<span class="input-group-addon">品牌名称</span>
										<input type="text" class="form-control" id="q_sub_brandName" name="brandName" placeholder="请输入品牌名称"/>
									</div>
								</div>
								<div class="col-sm-2" style="width: 200px;">
									<div class="input-group input-group-sm">
										<span class="input-group-addon">是否显示</span>
										<select id="q_sub_isShow" class="form-control">
											<option value="">--请选择--</option>
											<option value="0">未显示</option>
											<option value="1">已显示</option>
										</select>
									</div>
								</div>
								<div class="col-sm-2" style="width:140px;">
									<button type="button" id="btn_query_queryUnselectBrand" class="btn btn-primary">查询</button>&nbsp;&nbsp;
									<button type="reset" class="btn btn-success" id="resetBtn2" >重置</button>
								</div>
							</div>
						</form>
					</div>
				</div>
				<form id="form_assign"  method="post" action="${basePath}/back/brandType/saveSelectedBrandType.sjson">
					<input id="productTypeId" type="hidden" name="productTypeId" />
					<input id="brandIds" type="hidden" name="brandIds" />
					<input id="isShows" type="hidden" name="isShows" />
					<!-- 渲染的table -->
					<table id="tt_brandTypeList"></table>
				</form>
			</div>

			<div class="modal-footer">
				<button type="button" id="validateBtn" class="btn btn-primary"  onclick="saveBrandType()">确认关联</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>